import re


from django.http import JsonResponse

from apps.users.models import User

from django.views import View
# Create your views here.

class UsernameCountView(View):
    def get(self,request,username):
        # if not re.match('[a_zA_Z0_9_-]{5,20}',username):
        #     return JsonResponse({'code':200,'errmsg':'用户名不满足需求'})
        count=User.objects.filter(username=username).count()
        return JsonResponse({'code':0,'count':count,'errmsg':'ok'})


import json
class RegisterView(View):
    def post(self,request):
        body_bytes=request.body
        body_str=body_bytes.decode()
        body_dict=json.loads(body_str)

        username=body_dict.get('username')
        password=body_dict.get('password')
        password2=body_dict.get('password2')
        mobile=body_dict.get('mobile')
        allow=body_dict.get('allow')

        if not all ([username,password,password2,mobile,allow]):
            return JsonResponse({'code':400,'errmsg':'参数不全'})
        if not re.match('[a-zA-Z_-]{5,20}',username):
            return JsonResponse({'code':400,'errmsg':'用户名不满足规则'})
        if User.objects.filter(username=username).exists():
            return JsonResponse({'code': 400, 'errmsg': '该用户名已被注册'}, status=400)

        # User.objects.create(username=username,password=password,mobile=mobile)
        # #
        user=User.objects.create_user(username=username,password=password,mobile=mobile)

        from django.contrib.auth import login
        login(request, user)

        return JsonResponse({'code':0,'errmsg':'ok'})

class LoginView(View):
    def post(self,request):
        data=json.loads(request.body.decode())
        username=data.get('username')
        password=data.get('password')
        remembered=data.get('remembered')

        if not all([username,password]):
            return JsonResponse({'code':400,'errmsg':'参数不全'})

        if re.match('1[3-9]\d{9}',username):
            User.USERNAME_FIELD='mobile'
        else:
            User.USERNAME_FIELD='username'



        from django.contrib.auth import authenticate
        user=authenticate(username=username,password=password)
        if user is None:
            return JsonResponse({'code':400,'errmsg':'账户或者密码错误'})

        from django.contrib.auth import login
        login(request,user)
        if remembered:
            request.session.set_expiry(None)
        else:
            request.session.set_expiry(0)

        response=JsonResponse({'code':0,'errmsg':'ok'})

        response.set_cookie('username',username)
        return response



from django.contrib.auth import logout
class LogoutView(View):
    def delete(self,request):
        logout(request)
        response=JsonResponse({'code':0,'errmsg':'ok'})
        response.delete_cookie('username')
        return response




from utils.views import LoginRequiredJsonMixin
class CenterView(LoginRequiredJsonMixin,View):
    def get(self,request):
        info_data={
            'username':request.user.username,
            'email': request.user.email,
            'mobile': request.user.mobile,
            'email_active': request.user.email_active
        }

        return JsonResponse({'code':0,'errmsg':'ok','info_data':info_data})